package com.alpha.framework.fragment;

import com.alpha.framework.activity.BaseActivity;
import com.alpha.framework.manager.ActivityManager;
import com.alpha.framework.manager.FragmentManager;
import com.alpha.framework.utils.SystemUtils;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public abstract class BaseFragment extends Fragment {
	
	@Override
	public void onResume() {
		super.onResume();
	}
	
	@Override
	public void onPause() {
		super.onPause();
	}
	
	/**
	 * 获取当前处于前台的Fragment
	 * @return
	 */
	public static BaseFragment getForegroundFragment(){
		return (BaseFragment) FragmentManager.getManager().getCurrentFragment();
	}
	
	/**
	 * 删除集合中保存的Fragment 
	 */
	public static void finishFragment(){
		//TODO 不只是从集合移除
		FragmentManager.getManager().finishFragment();
	}
	
	/**
	 * 从保存Fragment 的集合中获取 对应的Fragment
	 * @param tag 集合中对应的 key
	 * @return 
	 */
	public static BaseFragment findFragment(String tag){
		return FragmentManager.getManager().findFragment(tag);
	}
	
	/**
	 * 删除Fragment
	 */
	public static void delFragment(String tag){
		FragmentManager.getManager().popFragment(tag);
	}
	
	/**
	 * 保存该Fragment 
	 */
	private void saveFragment(){
		FragmentManager.getManager().pushFragment(this);
	}
	
	
	//TODO 下面2个function implements
	
	/**
	 * title back 点击时的回调
	 */
	public void onTitleBackClick(){
		//使用模拟点击返回键回退（这样可以再Fragment回退栈为空时，回退Activity）
		SystemUtils.backCall();
		//mBaseFragmentActivity.getSupportFragmentManager().popBackStack();
	}

	/**
	 * title operation 点击时的回调
	 */
	public void onTitleOperationClick(){
	}
	
	protected BaseFragmentActivity mBaseFragmentActivity;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		//保存Fragment
		saveFragment();
		
		BaseActivity currentActivity = (BaseActivity) ActivityManager.getManager().getCurrentActivity();
		if(currentActivity instanceof BaseFragmentActivity){
			mBaseFragmentActivity=(BaseFragmentActivity)currentActivity;
		}
		
		initTitleBar();
		View createView = createView();
		init(createView);
		loadData();
		return createView;
	}
	
	/**
	 * 初始化数据
	 */
	protected abstract void loadData();
	
	/**
	 * 初始化 显示的Fragment View
	 * @return
	 */
	protected abstract View createView();
	
	/**
	 * 子类实现控件初始化
	 * @param createView 
	 */
	protected void init(View createView) {
		// TODO Auto-generated method stub
	}

	/**
	 * 初始化 titleBar 
	 */
	protected void initTitleBar() {
		setTitleOperation("");
	}

	/**
	 * 设置 title name
	 * @param title
	 */
	protected void setTitle(String title){
		if(null!=mBaseFragmentActivity)
			mBaseFragmentActivity.setTitle(title);
	}
	
	/**
	 * 设置 title operation 
	 * @param operationName
	 */
	protected void setTitleOperation(String operationName){
		if(null!=mBaseFragmentActivity)
			mBaseFragmentActivity.setTitleOperation(operationName);
	}
	
	/** 设置 title back 是否显示 */
	protected void setTitleBackVisible(int isVisibleMode){
		if(null!=mBaseFragmentActivity)
			mBaseFragmentActivity.setTitleBackVisible(isVisibleMode);
	}
	
	/**
	 * 切换Fragment
	 * 默认 将Fragment 加入回退栈
	 * 默认 Bundle 为null
	 * @param clazz
	 * @param bundle
	 */
	protected void switchFragment(Class<? extends BaseFragment> clazz){
		if(null!=mBaseFragmentActivity)
			mBaseFragmentActivity.switchFragment(true, clazz, null);
	}
	
	/**
	 * 切换Fragment
	 * 默认 将Fragment 加入回退栈
	 * @param clazz
	 * @param bundle
	 */
	protected void switchFragment(Class<? extends BaseFragment> clazz,Bundle bundle){
		if(null!=mBaseFragmentActivity)
			mBaseFragmentActivity.switchFragment(true, clazz, bundle);
	}
	
	/**
	 * 切换Fragment
	 * @param clazz
	 * @param bundle
	 */
	protected void switchFragment(boolean isAddBackStack,Class<? extends BaseFragment> clazz,Bundle bundle){
		if(null!=mBaseFragmentActivity)
			mBaseFragmentActivity.switchFragment(isAddBackStack, clazz, bundle);
	}
	
	
	/**
	 * 清除 保存的Fragment
	 * 初始化Fragment回退栈
	 */
	protected void clearFragmentStack() {
		if(null!=mBaseFragmentActivity)
			mBaseFragmentActivity.clearSaveFragment();
	}
}
